<template>
	<view class="search">
		<!-- 标题栏和状态栏占位符 -->
		<view class="titleNview-placing"></view>
		<view class="search-box">
			<w-search-t :mode="2" button="inside" :placeholder="defaultKeyword" @search="doSearch(false)"
			 @input="inputChange" @confirm="doSearch(false)" @clickLeft="back" v-model="keyword"></w-search-t>
		</view>
		<view class="search-keyword">
			<scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
				<block v-for="(row,index) in keywordList" :key="index">
					<view class="keyword-entry" hover-class="keyword-entry-tap">
						<view class="keyword-text" @tap.stop="doSearch(keywordList[index].keyword)">
							<rich-text :nodes="row.htmlStr"></rich-text>
						</view>
						<view class="keyword-img" @tap.stop="setKeyword(keywordList[index].keyword)">
							<view class="keyword_icon iconfont  icon-jiantou-copy-copy-copy" ></view>
						</view>
					</view>
				</block>
			</scroll-view>
			
			<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
				<template v-if="searchList.length === 0">
					<view class="keyword-block" v-if="oldKeywordList.length>0">
						<view class="keyword-list-header">
							<view>历史搜索</view>
							<view>
								<view class="keyword_icon iconfont icon-lajitong"  @tap="oldDelete"></view>
							</view>
						</view>
						<view class="keyword">
							<view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">{{keyword}}</view>
						</view>
					</view>
					<view class="keyword-block">
						<view class="keyword-list-header">
							<view>热门搜索</view>
							<view>
								<view class="keyword_icon iconfont" :class="'icon-yanjing'+ forbid"  @tap="hotToggle"></view>
							</view>
						</view>
						<view class="keyword" v-if="forbid==''">
							<view v-for="(keyword,index) in hotKeywordList" @tap="doSearch(keyword)" :key="index">{{keyword}}</view>
						</view>
						<view class="hide-hot-tis" v-else>
							<view>当前搜热门搜索已隐藏</view>
						</view>
					</view>
				</template>
				<template v-else>
					<!-- 数据列表 -->
					<block v-for="(item,index) in searchList" :key="index">
						<template v-if="type ==='post'">
							<!-- 视频 -->
							
						</template>
						<template v-else>
							<!-- 课程 -->
							
						</template>
					</block>
				</template>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	// 测试数据
	const demo1 = [
		{
			package_path: '',  //.cpcard,
			package_name: "10节课，让你快速从菜鸟到EP高手",
			package_desp:"一次线下培训课程+3个线上教学视频+500元优惠券",
			purchase_count:"99",
			true_price:"999.00",
			package_price:"1999.00",
			id:1
		},
		{
			package_path: '',  //.cpcard,
			package_name: "10节课，让你快速从菜鸟到EP高手",
			package_desp:"一次线下培训课程+3个线上教学视频+500元优惠券",
			purchase_count:"99",
			true_price:"999.00",
			package_price:"1999.00",
			id:1
		},
		{
			package_path: '',  //.cpcard,
			package_name: "10节课，让你快速从菜鸟到EP高手",
			package_desp:"一次线下培训课程+3个线上教学视频+500元优惠券",
			purchase_count:"99",
			true_price:"999.00",
			package_price:"1999.00",
			id:1
		}
		];
	
	const demo2 = [
		{
		image_path:'',  //.ccard,
		title: "10节课，让你快速从菜鸟到EP高手",
		time:"1小时25分钟",
		learn_count:"99",
		true_price:"999.00",
		price:"1999.00",
		id:1
		},{
		image_path:'',  //.ccard,
		title: "10节课，让你快速从菜鸟到EP高手",
		time:"1小时25分钟",
		learn_count:"99",
		true_price:"999.00",
		price:"1999.00",
		id:1
		},{
		image_path:'',  //.ccard,
		title: "10节课，让你快速从菜鸟到EP高手",
		time:"1小时25分钟",
		learn_count:"99",
		true_price:"999.00",
		price:"1999.00",
		id:1
		}]
	//引用epSearchT组件，如不需要删除即可
	import epSearchT from "@/components/ep-search/ep-search-t.vue"
	/* import comboList from '@/components/ep-search-list/combo-list.vue'
	import courseList from '@/components/ep-search-list/course-list.vue' */
	import wSearchT from "@/components/w-search/w-search-t.vue"
	export default {
		components: {
			//引用epSearchT组件，如不需要删除即可
			wSearchT,//comboList,courseList
		},
		data() {
			return {
				defaultKeyword: "",
				keyword: "",
				oldKeywordList: [],
				hotKeywordList: [],
				keywordList: [],
				forbid: '',
				isShowKeywordList: false,
				searchList:[],
				dataList_a:[
					{
						package_path: '',  //.cpcard,
						package_name: "10节课，让你快速从菜鸟到EP高手",
						package_desp:"一次线下培训课程+3个线上教学视频+500元优惠券",
						purchase_count:"99",
						true_price:"999.00",
						package_price:"1999.00",
						id:1
					},
					{
						package_path: '',  //.cpcard,
						package_name: "10节课，让你快速从菜鸟到EP高手",
						package_desp:"一次线下培训课程+3个线上教学视频+500元优惠券",
						purchase_count:"99",
						true_price:"999.00",
						package_price:"1999.00",
						id:1
					},
					{
						package_path: '',  //.cpcard,
						package_name: "10节课，让你快速从菜鸟到EP高手",
						package_desp:"一次线下培训课程+3个线上教学视频+500元优惠券",
						purchase_count:"99",
						true_price:"999.00",
						package_price:"1999.00",
						id:1
					}
				],
				dataList_b: [
					{
					image_path:'',  //.ccard,
					title: "10节课，让你快速从菜鸟到EP高手",
					time:"1小时25分钟",
					learn_count:"99",
					true_price:"999.00",
					price:"1999.00",
					id:1
					},{
					image_path:'',  //.ccard,
					title: "10节课，让你快速从菜鸟到EP高手",
					time:"1小时25分钟",
					learn_count:"99",
					true_price:"999.00",
					price:"1999.00",
					id:1
					},{
					image_path:'',  //.ccard,
					title: "10节课，让你快速从菜鸟到EP高手",
					time:"1小时25分钟",
					learn_count:"99",
					true_price:"999.00",
					price:"1999.00",
					id:1
					}
				],
				// 当前搜索类型
				type:"post"
			}
		},
		onLoad(e) {
			this.init();
			if (e.type) {
				this.type = e.type
			}
			let pageTitle = '视频'
			switch (this.type){
				case 'post':
				pageTitle = '视频'
					break;
				case 'course':
				pageTitle = '课程'
					break;
			}
		},
		methods: {
			back() {
				uni.navigateBack({
					delta:1
				})
			},
			init() {
				this.loadDefaultKeyword();
				this.loadOldKeyword();
				this.loadHotKeyword();

			},
			blur() {
				uni.hideKeyboard()
			},
			//加载默认搜索关键字
			loadDefaultKeyword() {
				//定义默认搜索关键字，可以自己实现ajax请求数据再赋值,用户未输入时，以水印方式显示在输入框，直接不输入内容搜索会搜索默认关键字
				this.defaultKeyword = "请输入搜索关键词";
			},
			//加载历史搜索,自动读取本地Storage
			loadOldKeyword() {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						this.oldKeywordList = OldKeys;
					}
				});
			},
			//加载热门搜索
			loadHotKeyword() {
				//定义热门搜索关键字，可以自己实现ajax请求数据再赋值
				this.hotKeywordList = ['免费公开课','开发者课程','组合课程','新手入门','宗师操作','大神进阶','免费'];
			},
			//监听输入
			inputChange(event) {
				//兼容引入组件时传入参数情况
				var keyword = event.detail ? event.detail.value : event;
				if (!keyword) {
					this.keywordList = [];
					this.isShowKeywordList = false;
					return;
				}
				this.isShowKeywordList = true;
				//以下示例截取淘宝的关键字，请替换成你的接口
				uni.request({
					url: 'https://suggest.taobao.com/sug?code=utf-8&q=' + keyword, //仅为示例
					success: (res) => {
						this.keywordList = [];
						this.keywordList = this.drawCorrelativeKeyword(res.data.result, keyword);

					}
				});
			},
			//高亮关键字
			drawCorrelativeKeyword(keywords, keyword) {
				var len = keywords.length,
					keywordArr = [];
				for (var i = 0; i < len; i++) {
					var row = keywords[i];
					//定义高亮#9f9f9f
					var html = row[0].replace(keyword, "<span style='color: #333;'>" + keyword + "</span>");
					html = '<div>' + html + '</div>';
					var tmpObj = {
						keyword: row[0],
						htmlStr: html
					};
					keywordArr.push(tmpObj)
				}
				return keywordArr;
			},
			//顶置关键字
			setKeyword(index) {
				this.keyword = this.keywordList[index].keyword;
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.oldKeywordList = [];
							uni.removeStorage({
								key: 'OldKeys'
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//热门搜索开关
			hotToggle() {
				this.forbid = this.forbid ? '' : '-bi';
			},
			//执行搜索
			doSearch(keyword) {
				keyword = keyword === false ? this.keyword : keyword;
				this.keyword = keyword;
				this.saveKeyword(keyword); //保存为历史 
				uni.showLoading({
					title: '加载中...',
					mask: false
				})
				/* uni.showToast({
					title: keyword,
					icon: 'none',
					duration: 2000
				}); */
				// 请求搜索
				setTimeout(()=>{
					switch (this.type){
						case 'post':
						this.searchList = demo1
							break;
						case 'course':
						this.searchList = demo2
							break;
					}
					// 隐藏loading
					uni.hideLoading()
				},200)
				
				//以下是示例跳转淘宝搜索，可自己实现搜索逻辑
				/*
				//#ifdef APP-PLUS
				plus.runtime.openURL(encodeURI('taobao://s.taobao.com/search?q=' + keyword));
				//#endif
				//#ifdef H5
				window.location.href = 'taobao://s.taobao.com/search?q=' + keyword
				//#endif
				*/
			},
			//保存关键字到历史记录
			saveKeyword(keyword) {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						var findIndex = OldKeys.indexOf(keyword);
						if (findIndex == -1) {
							OldKeys.unshift(keyword);
						} else {
							OldKeys.splice(findIndex, 1);
							OldKeys.unshift(keyword);
						}
						//最多10个纪录
						OldKeys.length > 10 && OldKeys.pop();
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					},
					fail: (e) => {
						var OldKeys = [keyword];
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					}
				});
			}
		}
	}
</script>
<style lang="scss">
	.titleNview-placing {
		height: var(--status-bar-height);
		box-sizing: content-box;
		background-color: #f4f4f4;
		display: block;
		position: sticky;
		top: 0rpx;
		z-index: 9999;
		overflow: hidden
	}
	.search{
		display: flex;
		width: 100%;
		flex-direction: column;
	}

	.search-box {
		height:117rpx;
		width: 100%;
		position: fixed;
		z-index: 999;
		background-color: #fff;
		box-shadow: 5rpx 5rpx 5rpx rgba(0,0,0,.1);
	}

	.search-box .input-box {
		width: 85%;
		flex-shrink: 1;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.search-box .search-btn {
		width: 15%;
		margin: 0 0 0 2%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-shrink: 0;
		font-size: 28rpx;
		color: #fff;
		background: linear-gradient(to right, #ff9801, #ff570a);
		border-radius: 60rpx;
	}

	.search-box .input-box>input {
		width: 100%;
		height: 60rpx;
		font-size: 32rpx;
		border: 0;
		border-radius: 60rpx;
		-webkit-appearance: none;
		-moz-appearance: none;
		appearance: none;
		padding: 0 3%;
		margin: 0;
		background-color: #ffffff;
	}

	.placeholder-class {
		color: #9e9e9e;
	}

	.search-keyword {
		display:flex;
		flex-direction: column;
		padding:140rpx 30rpx 30rpx 30rpx;
	}

	.keyword-list-box {
		height: calc(100vh - 110rpx);
		padding-top: 10rpx;
		border-radius: 20rpx 20rpx 0 0;
		background-color: #fff;
	}

	.keyword-entry-tap {
		background-color: #eee;
	}

	.keyword-entry {
		height: 80rpx;
		font-size: 30rpx;
		color: #333;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: solid 1rpx #e7e7e7;
	}

	.keyword-entry .keyword-text,
	.keyword-entry .keyword-img {
		height: 80rpx;
		display: flex;
		align-items: center;
	}

	.keyword-entry .keyword-text {
		width: 90%;
	}

	.keyword-entry .keyword-img {
		width: 10%;
		justify-content: center;
	}

	.keyword-box {
		height: calc(100vh - 110rpx);
		border-radius: 20rpx 20rpx 0 0;
		background-color: #fff;
	}

	.keyword-box .keyword-block {
		padding: 10rpx 0;
	}

	.keyword-box .keyword-block .keyword-list-header {
		width: 100%;
		flex:1;
		padding: 10rpx 0;
		font-size: 27rpx;
		color: #333;
		display: flex;
		justify-content: space-between;
	}
	.keyword_icon{
		color: #999;
	}

	.keyword-box .keyword-block .keyword {
		width: 94%;
		padding: 3px 3%;
		display: flex;
		flex-flow: wrap;
		justify-content: flex-start;
	}

	.keyword-box .keyword-block .hide-hot-tis {
		display: flex;
		justify-content: center;
		font-size: 28rpx;
		color: #6b6b6b;
	}

	.keyword-box .keyword-block .keyword>view {
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 60rpx;
		padding: 0 20rpx;
		margin: 10rpx 20rpx 10rpx 0;
		height: 60rpx;
		font-size: 28rpx;
		background-color: rgb(242, 242, 242);
		color: #6b6b6b;
	}
</style>
